package com.yangguang.huahui.ui.fragments;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.yangguang.huahui.R;
import com.yangguang.huahui.baseadapter.BaseQuickAdapter;
import com.yangguang.huahui.baseadapter.BaseViewHolder;
import com.yangguang.huahui.entity.DataInfo;
import com.yangguang.huahui.entity.FeedList;
import com.yangguang.huahui.mvp.presenter.FeedListPresenter;
import com.yangguang.huahui.mvp.view.FeedListView;
import com.yangguang.huahui.ui.activitys.BaseActivity;
import com.yangguang.huahui.ui.adapters.SelectAdapter;
import com.yangguang.huahui.utils.LogUtil;
import com.yangguang.huahui.utils.RxBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/3/15.
 */

public class FeedbackMessageFragment extends BaseMvpFragment<FeedListView, FeedListPresenter> implements FeedListView{

    public static FeedbackMessageFragment newInstance(int type) {
        FeedbackMessageFragment fragment = new FeedbackMessageFragment();
        Bundle arguments = new Bundle();
        arguments.putInt("type", type);
        fragment.setArguments(arguments);
        return fragment;
    }

    private int type = 1;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private FeedAdapter mAdapter;
    LinearLayoutManager layoutManager;
    private List<FeedList.ListBean> mList = new ArrayList<>();
    @Override
    protected int initLayoutId() {
        return R.layout.frag_message;
    }

    @Override
    protected void initView() {
        layoutManager = new LinearLayoutManager(mActivity);
        mRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new FeedAdapter(R.layout.feed_list_item, mList);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void onResume() {
        Observable<Integer> observable = RxBus.getInstance().register(this);
        observable.observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer i) {
                type = i;
                fetchData();
            }
        });
        super.onResume();
    }



    @Override
    protected void initData() {

    }

    @Override
    public void onSuccess(FeedList feedList) {
        mList.clear();
        if (feedList.getList() != null){
            mList = feedList.getList();
        }
        mAdapter.setNewData(mList);
    }

    @Override
    public void onError() {

    }

    @Override
    protected FeedListPresenter initPresenter() {
        return new FeedListPresenter();
    }

    @Override
    protected void fetchData() {
        mPresenter.getFeedList(DataInfo.TOKEN, type);
    }

    private class FeedAdapter extends BaseQuickAdapter<FeedList.ListBean>{

        public FeedAdapter(int layoutResId, List<FeedList.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FeedList.ListBean item) {
            helper.setText(R.id.tv_content, item.getContent());
        }
    }
}
